﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for AccountStatusDB
/// </summary>
/// 
namespace CE.User
{
    public class AccountStatusDB
    {

        public bool UpdatePasswordChangedDate(Int64 UserId, DateTime ChangedDate)
        {
            MyDatabase _db = new MyDatabase();
            SortedList _spList = new SortedList();
            _spList.Add("@CEID", UserId);
            _spList.Add("@LastPasswordChangedDate", ChangedDate);
            return _db.ExecuteNonSqlStatement("Update tblUserStatus Set DInfo1 = @LastPasswordChangedDate Where CEID = @CEID", _spList) > 0;
        }

        public int GetLastPasswordChagedStatus(Int64 UserId)
        {
            int _retValue = 0;
            DateTime _dtExpDate = CE.Global.General.GetUTCDateTime(); //DateTime.Now;
            MyDatabase _db = new MyDatabase();
            object _objDate = _db.ExecuteScalarSqlStatement("Select DInfo1 from tblUserStatus Where CEID =" + UserId.ToString());

            if (_objDate != DBNull.Value)
            {
                _dtExpDate = Convert.ToDateTime(_objDate);
                DateTime _dtLastTwoMonths = CE.Global.General.GetUTCDateTime().AddMonths(-2); //DateTime.Now.AddMonths(-2);

                if (DateTime.Compare(_dtExpDate, _dtLastTwoMonths) <= 0)
                {
                    _retValue = 1;
                }

            }
            else
            {
                _retValue = 1;
            }

            return _retValue;
        }

        public Int64 InsertAccountStatus(Int64 UserId, int UsrTypeId, Int16 IUStatus)
        {
            Int64 _result = -1;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserAccountStatusInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CEID", UserId);
            _sPara.Add("@UsrTypeId", UsrTypeId);
            _sPara.Add("@IUStatus", IUStatus);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public Int64 UpdateLoginStatus(Int64 UserId)
        {
            Int64 _result = -1;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserLoginStatusUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CEID", UserId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public AccountStatus GetAccountStatus(Int64 UserId, int UsrTypeId)
        {
            AccountStatus _accountstatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserAccountStatusSelectSingleItemByUserId";

            SortedList _spara = new SortedList();
            _spara.Add("@CEID", UserId);
            _spara.Add("@UsrTypeId", UsrTypeId);
            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _accountstatus = new AccountStatus();
                    _accountstatus = FillDataRecord(_dt);
                }
            }
            return _accountstatus;
        }

        public DataSet GetAccountStatus(string Email, string AuthCode)
        {
            DataSet _result = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserAccountStatusSelectSingleItemByEmailId";

            SortedList _spara = new SortedList();
            _spara.Add("@Email", MySecurity.Encrypt(Email, true));
            _spara.Add("@AuthCode", MySecurity.Encrypt(AuthCode, true));

            using (DataSet _ds = _db.GetDataSet(_spName, _spara))
            {
                if (_ds != null && _ds.Tables[0].Rows.Count > 0)
                {
                    _result = _ds;
                }
            }
            return _result;
        }

        private AccountStatus FillDataRecord(DataTable myDataTable)
        {
            AccountStatus myAccountStatus = new AccountStatus();
            myAccountStatus.UserStatusId = Convert.ToInt64(myDataTable.Rows[0]["UserStatusId"].ToString());
            myAccountStatus.CEID = Convert.ToInt64(myDataTable.Rows[0]["CEID"].ToString());
            myAccountStatus.IsConfirm = Convert.ToBoolean(myDataTable.Rows[0]["IsConfirm"].ToString());
            myAccountStatus.IsBlock = Convert.ToBoolean(myDataTable.Rows[0]["IsBlock"].ToString());
            myAccountStatus.IsLogin = Convert.ToBoolean(myDataTable.Rows[0]["IsLogin"].ToString());
            myAccountStatus.IsActive = Convert.ToBoolean(myDataTable.Rows[0]["IsActive"].ToString());
            myAccountStatus.SLoginCount = Convert.ToInt32(myDataTable.Rows[0]["SLoginCount"].ToString());
            myAccountStatus.FLoginCount = Convert.ToInt32(myDataTable.Rows[0]["FLoginCount"].ToString());
            return myAccountStatus;
        }

    }
}